import * as Router from "koa-router";
import {Restfull} from "../../config/resModel"
// import * as koaFrom from "formidable-upload-koa"
const route = new Router()
const koaFrom = require("formidable-upload-koa");
import { saveFile } from "../../utils/saveFile"
route.get("", ctx => {
    console.log(222);

    ctx.body = "haha"
})

const mid = async (ctx:any) => {
    console.log("访问了", ctx);
    const file = (ctx.req as any).files["file"]
    if (!file) {
        ctx.body = {
            msg: "上传失败",
            code: 1
        }
        return
    }
    // let file = ctx.req!.files
    const { size, path, name, type } = file
    console.log("ff", size, path, name, type);
    const res = await saveFile({ size, filePath: path, name, type })
    if(res) {
        ctx.body = {
            code:200,
            msg:"上传成功",
            data:{
                url:res
            }
        }
    }

}

route.post("/upload", koaFrom(), mid)


export {
    route
}